 <template>
  <div>
    <el-card class="box-card">
      <div>
        <el-row :gutter="8"
                type="flex"
                align="middle"
                style="margin-bottom:10px">
          <el-col :span="1.5">
            分类名字
          </el-col>
          <el-col :span="3">
            <el-input size="mini"
                      v-model='queryData.actName'
                      placeholder="请输入分类名字"></el-input>
          </el-col>
          <el-col :span="3">
            <el-select v-model="queryData.isStatus"
                       size="small"
                       placeholder="请选择">
              <el-option label="正常"
                         :value="1">
              </el-option>

              <el-option label="取消"
                         :value="2">
              </el-option>

            </el-select>
          </el-col>
          <el-col :span="1.5">
            <el-button size="mini"
                       type="primary"
                       icon="el-icon-search"
                       @click="getDate()">搜索</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon='el-icon-refresh'
                       size="mini"
                       @click="reset()">重置</el-button>
          </el-col>
        </el-row>
      </div>
      <el-row :gutter="8"
              type="flex"
              align="middle"
              class="btn_nav"
              style="margin-bottom:10px">
        <el-col :span="1.5">
          <el-button size="mini"
                     type="primary"
                     icon="el-icon-plus"
                     plain
                     @click="addact">新增</el-button>
        </el-col>

      </el-row>
      <el-table :data="tableData"
                v-loading="loading"
                style="width: 100%"
                :header-cell-style="{background:'#f8f8f9','text-align':'center'}"
                :cell-style="{'text-align':'center'}">

        <el-table-column prop="createTime"
                         label="添加时间">
        </el-table-column>
        <el-table-column prop="actName"
                         label="活动名称">
        </el-table-column>
        <el-table-column prop="startDate"
                         label="开始时间">

        </el-table-column>
        <el-table-column prop="endDate"
                         label="结束时间">

        </el-table-column>
        <el-table-column prop="address"
                         label="详细地址">
        </el-table-column>
        <el-table-column prop="sfMoney"
                         label="收费金额">
        </el-table-column>

        <el-table-column label="操作">
          <template v-slot="data">
            <el-button icon="el-icon-edit"
                       type="text"
                       @click="updateHandler(data.row.id)">修改</el-button>
            <el-button icon="el-icon-delete"
                       type="text"
                       @click="deleteHandler(data.row.id)">删除</el-button>
          </template>

        </el-table-column>

      </el-table>
      <div class="page"
           v-show="total!=0">
        <el-pagination background
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="queryData.pageNum"
                       :page-sizes="[3, 5, 7, 9]"
                       :page-size="queryData.pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="total">
        </el-pagination>
      </div>
    </el-card>

    <div>
      <el-dialog :title="flag?'添加用户活动':'修改用户活动'"
                 :visible.sync="dialogVisible"
                 width="30%">
        <el-form :model="formData"
                 ref="ruleForm"
                 label-width="80px">
          <el-form-item label="活动名称">
            <el-input size="small"
                      v-model="formData.actName"
                      placeholder="活动名称"></el-input>
          </el-form-item>

          <el-form-item label="开始时间">

            <el-date-picker v-model="formData.startDate"
                            size="small"
                            type="date"
                            placeholder="选择日期"
                            style="width: 100%;"></el-date-picker>

          </el-form-item>
          <el-form-item prop="vipExpireTime"
                        label="结束时间">

            <el-date-picker v-model="formData.endDate"
                            size="small"
                            type="date"
                            placeholder="选择日期"
                            style="width: 100%;"></el-date-picker>

          </el-form-item>
          <el-form-item label="详细地址"
                        prop="remark">
            <el-input size="small"
                      v-model="formData.address"
                      placeholder="详细地址"></el-input>
          </el-form-item>
          <el-form-item label="收费金额">
            <el-input size="small"
                      v-model="formData.sfMoney"
                      placeholder="收费金额"></el-input>
          </el-form-item>
          <el-form-item label="活动说明">
            <quill-editor :gz="gz"
                          @editorchange='getEidtorText' />

          </el-form-item>

          <el-form-item label="状态"
                        prop="ty">
            <el-select placeholder="请选择"
                       v-model="formData.isStatus">
              <el-option label="正常"
                         :value="1">
              </el-option>
              <el-option label="取消"
                         :value="2">
              </el-option>

            </el-select>
          </el-form-item>

          <el-form-item label="封面">
            <el-upload class="avatar-uploader"
                       action="https://jumeng.xzjumeng.com/prod-api/common/upload"
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess">
              <img v-if="formData.fmPic"
                   :src="formData.fmPic"
                   class="avatar">
              <i v-else
                 class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="报名人数上限">
            <el-input size="small"
                      v-model="formData.bmAllNum"
                      placeholder="报名人数上限"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="update">确 定</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>


<script>
import quillEditor from '../../../components/Quill'
export default {
  components: { quillEditor },
  data() {
    return {
      queryData: {
        pageNum: 1,
        pageSize: 5,
        actName: null,
        isStatus: null
      },
      loading: false,
      tableData: [],
      total: 0,
      flag: false,
      dialogVisible: false,
      formData: {
        actName: null,
        actSm: null,
        address: null,
        areaId: null,
        areaName: null,
        bmAllNum: null,
        bmNum: null,
        cityId: null,
        cityName: null,
        countryId: null,
        countryName: null,
        createTime: null,
        endDate: null,
        fmPic: null,
        id: null,
        isStatus: 1,
        proId: null,
        proName: null,
        remark: null,
        sfMoney: null,
        startDate: null,
        updateTime: null
      },
      gz: ''
    }
  },
  mounted() {
    this.getDate()
  },
  methods: {
    async getDate() {
      this.loading = true
      let { data } = await this.$axios.get('com/activity/list', { params: this.queryData })
      console.log(data)
      this.tableData = data.rows
      this.total = data.total
      this.loading = false
    },
    handleSizeChange(val) {
      this.queryData.pageSize = val
      this.getDate()
    },
    handleCurrentChange(val) {
      this.queryData.pageNum = val
      this.getDate()
    },
    search() {
      this.getDate()
    },
    reset() {
      this.queryData = {
        pageNum: 1,
        pageSize: 5,
        actName: null,
        isStatus: null
      }
    },
    deleteHandler(id) {
      this.$confirm(`是否确认删除id为?${id}的数据项`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let { data } = await this.$axios.delete(`com/activity/${id}`)
          if (data.code == 200) {
            console.log(data)
            this.$message({ type: 'success', message: data.msg })
            this.getDate()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async updateHandler(id) {
      this.dialogVisible = true
      this.flag = false
      let { data } = await this.$axios.get(`com/activity/${id}`)
      this.formData = data.data
      this.gz = ''
    },
    addact() {
      this.formData = {
        actName: null,
        actSm: null,
        address: null,
        areaId: null,
        areaName: null,
        bmAllNum: null,
        bmNum: null,
        cityId: null,
        cityName: null,
        countryId: null,
        countryName: null,
        createTime: null,
        endDate: null,
        fmPic: null,
        id: null,
        isStatus: 1,
        proId: null,
        proName: null,
        remark: null,
        sfMoney: null,
        startDate: null,
        updateTime: null
      }
      this.dialogVisible = true
      this.flag = true
      this.gz = ''
    },
    update() {
      this.formData.actSm = this.editor.html
      if (this.editor) {
        this.formData.actSm = this.editor.html
      }
      this.$refs.ruleForm.validate(async (valid) => {
        let data = null
        if (!valid) return false
        if (!this.formData.id) {
          data = await this.$axios.post('com/activity', this.formData)
        } else {
          data = await this.$axios.put('com/activity', this.formData)
        }

        if (data.data.code != 200) return this.$message.error(data.data.msg)
        this.$message({
          message: data.data.msg,
          type: 'success'
        })
        this.dialogVisible = false
        this.getDate()
      })
    },
    // 富文本
    getEidtorText({ html, _this }) {
      if (!this.editor) this.editor = _this
      this.editor.html = html
    },
    handleAvatarSuccess(res, file) {
      this.formData.fmPic = res.url
    }
  }
}
</script>

<style>
.page {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>